import { createRouter, createWebHistory } from "vue-router";
import Login from "../login/Login.vue";
import Home from "../home/Home.vue";
import Edit from "../home/edit.vue";
import Account from "../admin/account.vue";
import Profile from "../member/profile.vue";
import Book from "../member/book.vue";
import Characters from "../member/characters.vue";
import ApproveProfile from "../admin/approveProfile.vue";
import ApproveBook from "../admin/approveBook.vue";
import ApproveCharacters from "../admin/approveCharacters.vue";
import ManageProfile from "../admin/manageProfile.vue";
import ManageBook from "../admin/manageBook.vue";
import ManageCharacters from "../admin/manageCharacters.vue";
import PageAssets from "../admin/pageAssets.vue";
import ManageVideo from "../admin/manageVideo.vue";
import ManageAnnouncement from "../admin/manageAnnouncement.vue";

const routes = [
  {
    path: "/",
    redirect: "/login",
  },
  {
    path: "/login",
    name: "Login",
    component: Login,
  },
  {
    path: "/home",
    name: "Home",
    component: Home,
    children: [
      {
        path: "admin/account",
        name: "Account",
        component: Account,
      },
      {
        path: "admin/approveBook",
        name: "ApproveBook",
        component: ApproveBook,
      },
      {
        path: "admin/approveProfile",
        name: "ApproveProfile",
        component: ApproveProfile,
      },
      {
        path: "admin/approveCharacters",
        name: "ApproveCharacters",
        component: ApproveCharacters,
      },
      {
        path: "admin/manageProfile",
        name: "ManageProfile",
        component: ManageProfile,
      },
      {
        path: "admin/manageCharacters",
        name: "ManageCharacters",
        component: ManageCharacters,
      },
      {
        path: "admin/manageVideo",
        name: "ManageVideo",
        component: ManageVideo,
      },
      {
        path: "admin/pageAssets",
        name: "PageAssets",
        component: PageAssets,
      },
      {
        path: "admin/manageBook",
        name: "ManageBook",
        component: ManageBook,
      },
      {
        path: "admin/manageAnnouncement",
        name: "ManageAnnouncement",
        component: ManageAnnouncement,
      },
      {
        path: "edit",
        name: "Edit",
        component: Edit,
      },
      {
        path: "member/profile",
        name: "Profile",
        component: Profile,
      },
      {
        path: "member/book",
        name: "Book",
        component: Book,
      },
      {
        path: "member/characters",
        name: "Characters",
        component: Characters,
      },
    ],
  },
];

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: routes,
});

// 路由守卫：未登录则跳转到登录页
router.beforeEach((to, from, next) => {
  const userId = sessionStorage.getItem("user_id");
  const token = sessionStorage.getItem("token");
  if (to.path !== "/login" && !userId && !token) {
    next("/login");
  } else {
    next();
  }
});
export default router;
